import {defHttp} from '/@/utils/http/axios';
import {Modal} from 'ant-design-vue';

enum Api {
  saveGroup = '/infra-service/materialGroup/save',
  updateGroup = '/infra-service/materialGroup/update',
  batchDeleteGroup = '/infra-service/materialGroup/batchDelete',
  deleteGroup = '/infra-service/materialGroup/delete',

  page = '/infra-service/material/page',
  list = '/infra-service/material/list',
  save = '/infra-service/material/save',
  edit = '/infra-service/material/update',
  delete = '/infra-service/material/delete',
  deleteBatch = '/system-service/material/batchDelete'
}

export const saveOrUpdateGroup = (params, isUpdate) => {
  let url = isUpdate ? Api.updateGroup : Api.saveGroup;
  return defHttp.post({url: url, params});
}

export const deleteGroup = (params, handleSuccess) => {
  Modal.confirm({
    title: `确认删除 ${params.name}`,
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({url: Api.deleteGroup, data: { id: params.id }}, {joinParamsToUrl: true}).then(() => {
        handleSuccess();
      });
    }
  });
}

export const batchDeleteGroup = (groupName, params, handleSuccess) => {
  Modal.confirm({
    title: `确认删除 ${groupName}`,
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({url: Api.batchDeleteGroup, data: params}, {joinParamsToUrl: true}).then(() => {
        handleSuccess();
      });
    }
  });
}

export const pageMaterial = (params) => defHttp.get({url: Api.page, params});
export const listMaterial = (params) => defHttp.get({url: Api.list, params});

export const deleteMaterial = (params, handleSuccess) => {
  return defHttp.delete({url: Api.delete, params}, {joinParamsToUrl: true}).then(() => {
    handleSuccess();
  });
}
export const batchDeleteMaterial = (params, handleSuccess) => {
  Modal.confirm({
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({url: Api.deleteBatch, data: params}, {joinParamsToUrl: true}).then(() => {
        handleSuccess();
      });
    }
  });
}
export const saveOrUpdateMaterial = (params, isUpdate) => {
  let url = isUpdate ? Api.edit : Api.save;
  return defHttp.post({url: url, params});
}
